﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControls_TH_XemChiTietSanPham_Case : System.Web.UI.UserControl
{
    private DataSet objDataSet;
    private String filename;
    private string id;
    protected void Page_Load(object sender, EventArgs e)
    {
        SelectChiTietSanPham();
    }

    public void SelectChiTietSanPham()
    {
        id = Request.QueryString["id"];
        if (id != null)
        {
            DataSet objDataDS;
            SanPhamBUS objSanPham = new SanPhamBUS(TGDDConfiguration.DbConnectionString);
            objDataDS = objSanPham.SelectSanPham(int.Parse(id));

            DataRow row = objDataDS.Tables[0].Rows[0];
            filename = row["HinhMinhHoa"].ToString();

            repSanPham.DataSource = objDataDS.Tables["SelectSanPham"];
            repSanPham.DataBind();
        }
    }

    public DataSet NewHinhDataSet()
    {
        DataSet objDataSet = new DataSet();
        DataTable objDataTable = objDataSet.Tables.Add("HinhMinhHoa");

        DataColumn objDataColumn;

        objDataColumn = new DataColumn("Hinh", Type.GetType("System.String"));
        objDataTable.Columns.Add(objDataColumn);

        objDataColumn = new DataColumn("width", Type.GetType("System.Int32"));
        objDataTable.Columns.Add(objDataColumn);

        objDataColumn = new DataColumn("height", Type.GetType("System.Int32"));
        objDataTable.Columns.Add(objDataColumn);

        return objDataSet;
    }

    protected void repSanPham_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        RepeaterItem item = e.Item;
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater repHinhMinhHoa = (Repeater)item.FindControl("repHinhMinhHoa");

            objDataSet = NewHinhDataSet();
            DataRow myDataRow = objDataSet.Tables["HinhMinhHoa"].NewRow();

            //lay kich thuoc image, sau do thuc hien resize
            int fileWidth = 0;
            int fileHeight = 0;
            GlobalFunction.GetImageSize(ref fileWidth, ref fileHeight, Server.MapPath(".") + "\\images\\products\\" + filename);

            GlobalFunction.ResizeImage(ref fileWidth, ref fileHeight, TGDDConfiguration.FixSize);
            ///////////////////////////////
            myDataRow["Hinh"] = filename;
            myDataRow["width"] = fileWidth;
            myDataRow["height"] = fileHeight;
            objDataSet.Tables["HinhMinhHoa"].Rows.Add(myDataRow);

            repHinhMinhHoa.DataSource = objDataSet.Tables[0];
            repHinhMinhHoa.DataBind();
            //////////////////////////
            Repeater repKhuyenMai = (Repeater)item.FindControl("repKhuyenMai");
            string id = Request.QueryString["id"];
        }
    }

    protected void LikeRating_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
    {
        SanPhamBUS objSanPham = new SanPhamBUS(TGDDConfiguration.DbConnectionString);
        objSanPham.UpdateDiemSo(int.Parse(id), int.Parse(e.Value));
    }

    private void MessageBox(string msg)
    {
        Page.Controls.Add(new LiteralControl(
         "<script language='javascript'> window.alert('" + msg.Replace("'", "\\'") + "');</script>"));
    }

    protected void repSanPham_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if(Session["g_UserId"] == null)
        {
            MessageBox("Vui lòng đăng nhập để mua hàng!");
            return;
        }
        // Add the product to the shopping cart
        GioHangBUS objShoppingCart =
            new GioHangBUS(TGDDConfiguration.DbConnectionString);
        GioHangDTO shoppingCartDto = new GioHangDTO();
        shoppingCartDto.Id = AccessGioHang.idGioHang;
        shoppingCartDto.IdSanPham = int.Parse(id);

        try
        {
            // Execute the insert command
            bool success = objShoppingCart.AddItemToGioHang(shoppingCartDto);
        }
        catch (System.Exception ex)
        {
            throw new Exception(ex.Message, ex.InnerException);
        }
    }
}
